首页 > 其他 > 详细

Codeforces#498F. Xor-Paths(折半搜索)

时间:2018-07-17 15:24:55      阅读:189      评论:0      收藏:0      [点我收藏+]
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 (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.

Input

The first line of the input contains three integers nn, mm and kk (1n,m201≤n,m≤20, 0k10180≤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 (0ai,j10180≤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).

 

题意:从$(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(折半搜索)

原文:https://www.cnblogs.com/zwfymqz/p/9323274.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!