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

2018-07-18 01:15:21来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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
*/

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:2018/7/16 YMOI模拟 NOIP2013D2T3华容道

下一篇:codeforces757E. Bash Plays with Functions(狄利克雷卷积 积性