寒假训练——搜索 G - Xor-Paths

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
3 3 11
2 1 5
7 10 0
12 6 4
Output
3
Input
3 4 2
1 3 3 3
0 3 3 2
3 0 1 1
Output
5
Input
3 4 1000000000000000000
1 3 3 3
0 3 3 2
3 0 1 1
Output
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) .

 

 

题目:G - Xor-Paths
思路:
折半思想,前一半:从位置(1,1)开始到x+y=(n+m)/2,可以看成函数,进行异或。
后一半:从(n,m)开始,一直到x+y=(n+m)/2,异或。
异或有交换律,还有其他运算法则。
由运算法则可以推出公式
k=a1^a2^...^an;
令i=(1+n)/2;
q=a1^a2^...^ai;
sum=ai^...^an;
k=sum^ai^q;
所以q=sum^ai^k;

然后你第一个函数在走到的点给sum值打个标记,然后第二个函数把sum^ai就是取消这一步的值(这个值在前一个函数)
再^k就是找到跟现在这个sum异或起来为k(也就是合成一条路径)标记的值,也就是方案有多少
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=25;
ll a[maxn][maxn],ans,k;
map<ll,ll>mp[maxn];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int n,m;
void dfs_pre(int x,int y,ll sum)
{
    if(x+y==(n+m+2)/2) {mp[x][sum]++;return ;}//x+y==(n+m+2)/2,有个+2是因为有两种特殊情况,一个是n=1,一个是m=1
    for(int i=0;i<2;i++)
    {
        int tx=x+dx[i];
        int ty=y+dy[i];
        if(tx<1||ty<1||tx>n||ty>m) continue;
        dfs_pre(tx,ty,sum^a[tx][ty]);
    }
}
void dfs_end(int x,int y,ll sum)
{
    if(x+y==(n+m+2)/2) {ans+=mp[x][sum^k^a[x][y]];return ;}
    for(int i=2;i<4;i++)
    {
        int tx=x+dx[i];
        int ty=y+dy[i];
        if(tx<1||ty<1||tx>n||ty>m) continue;
        dfs_end(tx,ty,sum^a[tx][ty]);
    }
}

int main()
{
    scanf("%d%d%I64d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%I64d",&a[i][j]);
        }
    }
    dfs_pre(1,1,a[1][1]);
    dfs_end(n,m,a[n][m]);
    printf("%I64d\n",ans);
    return 0;
}

  

 

转载于:https://www.cnblogs.com/EchoZQN/p/10351453.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值