codeforces 1006 F(折半搜索)

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

 

/*
暴搜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;
}

 

 

转载于:https://www.cnblogs.com/L-Memory/p/9890727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值