Editorial
Number the colors Red, Orange, Yellow, Blue, Purple as , and represent the state by or . Use the following mapping :
For a digit , is the unique integer from to satisfying both conditions below.
- is the color index of .
- is the state of .
Color composition is addition of color indices modulo , while XOR of two states is addition modulo . Therefore, by the Chinese remainder theorem,
Let be the digit at the position of . Define a prefix sum for every decimal position by
The result at decimal position for a subarray is if and only if .
The whole weird decimal XOR is exactly when this condition holds at every decimal position. Since , only the positions matter. Define
Then the weird decimal XOR of is if and only if .
Subtask 1
If , only the ones digit can be nonzero, so each is effectively a single residue modulo . Store prefix occurrence counts for each residue . In a query , obtain the frequency of every residue among prefix positions . If a residue appears times, it contributes equal-position pairs. Thus one query takes time.
The time complexity is and the space complexity is .
Full constraints
Now is a vector of length , so the entire state space is far too large to enumerate. However, only can actually occur. Sort these vectors lexicographically and assign the same integer ID to equal vectors.
Each query is now the problem of counting pairs of distinct positions with equal IDs inside the prefix-position interval .
Process these queries offline with Mo's algorithm. If the current interval contains occurrences of ID , adding one more increases the answer by . When removing , first decrease its frequency by one and then subtract the remaining frequency from the answer.
Building the vectors costs , coordinate compression costs , and Mo's algorithm costs . Therefore the total time complexity is
and the space complexity is .
Solution written by GPT5.6