F. Xor-Paths
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,m) meeting the following constraints:
- You can move to the right or to the bottom only. Formally, from the cell (i+1,j). The target cell can't be outside of the grid.
- The xor of all the numbers on the path from the cell (
Find the number of such paths in the given grid.
Input
The first line of the input contains three integers k.
The next 0≤ai,j≤1018).
Output
Print one integer — the number of paths from (k.
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)→(2,2)→(2,3)→(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)→(2,2)→(3,2)→(3,3)→(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)→(1,3)→(2,3)→(3,3)→(3,4)
/* 暴搜2^(n+m) 折半搜索 */ #include<bits/stdc++.h> #define N 27 #define ll long long using namespace std; ll n,m,k,ans,flag; ll a[N][N]; map<ll,ll>M[N][N]; inline ll read() { ll x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(x=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } void dfs(int dep,int x,int y,ll sta) { if(x<1 || x>n || y<1 || y>m) return; if(!flag) sta^=a[x][y]; if(x+y==dep) { if(!flag){M[x][y][sta]++;return;} else{ans+=M[x][y][k^sta];return;} } if(!flag){ dfs(dep,x+1,y,sta);dfs(dep,x,y+1,sta); } else{ sta^=a[x][y]; dfs(dep,x-1,y,sta);dfs(dep,x,y-1,sta); } } int main() { n=read();m=read();k=read(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=read(); flag=0;dfs((n+m+2)/2,1,1,0); flag=1;dfs((n+m+2)/2,n,m,0); printf("%lld\n",ans); return 0; }