Codeforces#498F. Xor-Paths(折半搜索)
Posted zwfymqz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces#498F. Xor-Paths(折半搜索)相关的知识,希望对你有一定的参考价值。
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 (xor operation 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.
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).
Print one integer — the number of paths from (1,11,1) to (n,mn,m) with xor sum equal to kk.
3 3 11
2 1 5
7 10 0
12 6 4
3
3 4 2
1 3 3 3
0 3 3 2
3 0 1 1
5
3 4 1000000000000000000
1 3 3 3
0 3 3 2
3 0 1 1
0
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).
题意:从$(1, 1)$走到$(n, m)$,路径上权值异或起来为$k$的有几条
昨晚前五题都1A之后有点上天qwq。。想了很久才发现这是个思博题不过没时间写了qwq。
考虑如果直接dfs的话是$2^{n + m}$
然后meet in the middle 一下就好了
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> #include<map> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> using namespace __gnu_pbds; #define MP(x, y) make_pair(x, y) #define Pair pair<int, int> #define int long long using namespace std; const int MAXN = 2 * 1e5 + 10, INF = 1e9 + 10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < ‘0‘ || c > ‘9‘) {if(c == ‘-‘) f = -1; c = getchar();} while(c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘, c = getchar(); return x * f; } int N, M, K; int a[21][21]; cc_hash_table<int, int> mp[21]; int dfs(int x, int y, int now) { if(x < 1 || x > N || y < 1 || y > M) return 0; if(x + y == (N + M + 2) / 2) return mp[x][now ^ a[x][y]]; int ans = 0; ans += dfs(x - 1, y, now ^ a[x - 1][y]); ans += dfs(x, y - 1, now ^ a[x][y - 1]); return ans; } void fuck(int x, int y, int now) { if(x < 1 || x > N || y < 1 || y > M) return ; if(x + y == (N + M + 2) / 2) {mp[x][now]++; return ;} fuck(x + 1, y, now ^ a[x + 1][y]); fuck(x, y + 1, now ^ a[x][y + 1]); } main() { #ifdef WIN32 freopen("a.in", "r", stdin); #endif N = read(); M = read(); K = read(); for(int i = 1; i <= N; i++) for(int j = 1; j <= M; j++) a[i][j] = read(); fuck(1, 1, a[1][1]); printf("%lld", dfs(N, M, K ^ a[N][M])); } /* 1 1 1000000000000000000 1000000000000000000 */
以上是关于Codeforces#498F. Xor-Paths(折半搜索)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #498 (Div. 3)
Codeforces Round #498 (Div. 3)
Codeforces Round #498 (Div. 3) 简要题解