一 原题
F. Xor-Paths
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a rectangular grid of size n×mn×m. Each cell has a number written on it; the number on the cell (i,ji,j) is ai,jai,j. Your task is to calculate the number of paths from the upper-left cell (1,11,1) to the bottom-right cell (n,mn,m) meeting the following constraints:
- You can move to the right or to the bottom only. Formally, from the cell (i,ji,j) you may move to the cell (i,j+1i,j+1) or to the cell (i+1,ji+1,j). The target cell can't be outside of the grid.
- The xor of all the numbers on the path from the cell (1,11,1) to the cell (n,mn,m) must be equal to kk (xoroperation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal).
Find the number of such paths in the given grid.
Input
The first line of the input contains three integers nn, mm and kk (1≤n,m≤201≤n,m≤20, 0≤k≤10180≤k≤1018) — the height and the width of the grid, and the number kk.
The next nn lines contain mm integers each, the jj-th element in the ii-th line is ai,jai,j (0≤ai,j≤10180≤ai,j≤1018).
Output
Print one integer — the number of paths from (1,11,1) to (n,mn,m) with xor sum equal to kk.
Examples
input
Copy
3 3 11 2 1 5 7 10 0 12 6 4
output
Copy
3
input
Copy
3 4 2 1 3 3 3 0 3 3 2 3 0 1 1
output
Copy
5
input
Copy
3 4 1000000000000000000 1 3 3 3 0 3 3 2 3 0 1 1
output
Copy
0
Note
All the paths from the first example:
- (1,1)→(2,1)→(3,1)→(3,2)→(3,3)(1,1)→(2,1)→(3,1)→(3,2)→(3,3);
- (1,1)→(2,1)→(2,2)→(2,3)→(3,3)(1,1)→(2,1)→(2,2)→(2,3)→(3,3);
- (1,1)→(1,2)→(2,2)→(3,2)→(3,3)(1,1)→(1,2)→(2,2)→(3,2)→(3,3).
All the paths from the second example:
- (1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4);
- (1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4);
- (1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4)(1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4);
- (1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4);
- (1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4).
二 分析
题意:给你一个n*m的矩阵,n和m不超过20,要求你找到满足两个条件的路径总数。条件1:从(1, 1)出发,(n,m)结束,只向右和向下走;条件2:路径上所有数的异或值为k,k最大是10^18。
分析:从(1, 1)到(n, m)的路径共有条,m和n均取20的时候有10^11种情况,显然太慢了。我们把坐标和为 的点成为半程点。正解是把任务分为两次搜索:
1. 从(1, 1)搜到半程点,对于每个半程点,统计一下可能的路径异或值和对应的路径数量,用map存储;
2. 从(n, m)搜到半程点,搜到时如果此时需要的异或值为x,就加上这个半程点出异或值为x的路径数目。
三 代码
import java.io.*;
import java.util.*;
public class Main {
static int n, m;
static long k;
static long[][] a = new long[25][25];
static List<Map<Long, Long>> map = new ArrayList<>();
public static void main(String[] args) throws FileNotFoundException {
// InputStream inputStream = new FileInputStream("src/input.txt");
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
n = in.nextInt();
m = in.nextInt();
k = in.nextLong();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = in.nextLong();
}
}
for (int i = 1; i <= 25; i++) {
map.add(new HashMap<>());
}
dfs1(1, 1, 0);
long ans = dfs2(n, m, k);
out.println(ans);
out.close();
}
static void dfs1(int x, int y, long s) {
if (x > n || y > m) return;
long xorVal = s ^ a[x][y];
if ((x + y) == (n + m) / 2 + 1) {
map.get(x).put(xorVal, map.get(x).getOrDefault(xorVal, (long)0) + 1);
return;
}
dfs1(x + 1, y, xorVal);
dfs1(x, y + 1, xorVal);
}
static long dfs2(int x, int y, long s) {
if (x < 1 || y < 1) return 0;
long xorVal = s ^ a[x][y];
if ((x + y) == (n + m) / 2 + 1) {
return map.get(x).getOrDefault(s, (long)0);
}
else return dfs2(x - 1, y, xorVal) + dfs2(x, y - 1, xorVal);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String ret;
try {
ret = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return ret;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}