codeforce 1006 F. Xor-Paths(双向bfs)

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×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 (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).

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).
思路:
以limt为边界两边枚举,到交接处判断一下结果
代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll>mp[22][22];
ll a[22][22];
int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
    int x,y;
    ll bit;
};
queue<node>P;
int main()
{
    int n,m;ll k;scanf("%d%d%lld",&n,&m,&k);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        scanf("%lld",&a[i][j]);
    if(n==1&&m==1)
    {
        if(a[1][1]==k) printf("1\n");
        else printf("0\n");
        return 0;
    }
    int limt=(n+m)/2;
    node e;
    e.x=1,e.y=1;e.bit=a[1][1];
    mp[1][1][a[1][1]]++;
    P.push(e);
    while(!P.empty())
    {
        e=P.front();P.pop();
        for(int i=0;i<2;i++)
        {
            int x=e.x+d[i][0],y=e.y+d[i][1];
            if((x+y)<=limt&&x<=n&&y<=m)
            {
                node r;
                r.x=x;r.y=y;r.bit=e.bit^a[x][y];
                mp[x][y][r.bit]++;
                P.push(r);
            }
        }
    }
    ll sum=0;
    e.x=n;e.y=m;e.bit=a[n][m];
    //sum+=mp[n][m][0];
    P.push(e);
    while(!P.empty())
    {
        e=P.front();P.pop();
        for(int i=2;i<4;i++)
        {
            int x=e.x+d[i][0],y=e.y+d[i][1];
            if((x+y)>=limt&&x>=1&&y>=1)
            {
                node r;
                r.x=x;r.y=y;r.bit=e.bit^a[x][y];
                sum+=mp[x][y][e.bit^k];
                P.push(r);
            }
        }
    }
    printf("%lld\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/81090077