Codeforces#498F. Xor-Paths(折半搜索)
2018-07-18 01:15:21来源:博客园 阅读 ()
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 */
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash