标签:fir cstring class www. htm rect with sid sam
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:
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:
All the paths from the second example:
题意:从$(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(折半搜索)
标签:fir cstring class www. htm rect with sid sam
原文地址:https://www.cnblogs.com/zwfymqz/p/9323274.html