Official Analysis (in Romanian)

Explanation

After running the brute force solution, one can observe that the period of how matrix looks like is the smallest power of two such that it's greater than both nn and mm.

Also, we can reduce the problem to finding the xor-sum of the elements in positions such that the number of roads from (0,0)(0, 0) to (i,j)(i, j) in kk steps is odd, where kk is the number of transformations done.

This is equal to C(k,i)C(k,j)C(k, i) \cdot C(k, j), where C(n,k)C(n, k) is nn choose kk. Also, if C(n,k)C(n, k) is odd, then n&k=kn \& k = k (this can also be observed by brute-force).

Also, if C(n,i)C(n, i) and C(n,j)C(n, j) are odd, then C(n,ij)C(n, i|j) is odd as well, so we can reduce the problem to SOS DP, where SOS[i]SOS[i] = xorsum of elements which are in positions included by ii.

For each bit of the period, SOS[i]=SOS[i2position]SOS[i]SOS[i] = SOS[i - 2^{position}] \oplus SOS[i], if ii has the bit on position equal to 11.

For other approaches, you can also check out the user solutions on Infoarena, just make sure to press that popup you receive after you first click on a solution. Take note that the version used is slightly different from the actual version, as this problem was given as an interactive problem and Infoarena couldn't support interactive problems at the time.

Implementation for infoarena version

Time Complexity: O(NMlogN)\mathcal{O}(N \cdot M \cdot \log N)

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream f("xortransform.in");
int n, m, q;
f >> n >> m >> q;
vector<int> sos(n * m * 2 + 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!