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
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;
}

  

寒假训练——搜索 G - Xor-Paths的更多相关文章

  1. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  2. 寒假训练——搜索 E - Bloxorz I

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  3. 寒假训练——搜索——C - Robot

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  4. J - Abbott's Revenge 搜索 寒假训练

    题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...

  5. 寒假训练 A - A Knight's Journey 搜索

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  6. 算法专题训练 搜索a-T3 Ni骑士(ni)

    搞了半天八数码弄不出来就只好来打题解  这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...

  7. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  8. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  9. 2016huasacm暑假集训训练三 G - 还是畅通工程

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...

随机推荐

  1. Jenkins CLI 命令详解

    笔者在前文<通过 CLI 管理 Jenkins Server>中介绍了如何通过 SSH 或客户端命令行的方式管理 Jenkins Server,限于篇幅,前文主要的目的是介绍连接 Jenk ...

  2. WebService与RMI(远程调用方式实现系统间通信)

    前言 本文是<分布式java应用基础与实践>读书笔记:另外参考了此博客,感觉讲的挺好的,尤其是其中如下内容: 另外,消息方式实现系统间通信本文不涉及.RMI则只采用spring RMI框架 ...

  3. 使用flexible适配移动端h5页面

    flexible是淘宝提供的一套REM手机适配的库,用法也非常简单 首先,在页面中引入相关资源 包括flexible.js和flexible_css.js(用于清除默认样式),或者通过cdn方式引入 ...

  4. js对象深拷贝与浅拷贝

    浅拷贝 把a赋值给b,a与b指向相同的内存,修改b值,a也会跟着改变. var a = "aa"; var b = a; b = "bb"; 这个时候a也变成了 ...

  5. Ocelot中文文档-Route

    路由(Routing) Ocelot主要功能是接收即将发来的请求并转发它们至下游服务.与此同时,以另一个http请求的形式(在将来这可能是任何传输的机制) Ocelot将一个请求的路由描述为另一个路由 ...

  6. JavaSE 异常抛光解析

    异常 异常指的是程序中的不正常现象,一般异常都是由第三方数据的使用造成的.java中每种异常现象都会有一个对应的异常类.java对异常的处理方式就是终止程序.异常机制其实是为了帮助我们找到程序中的问题 ...

  7. Python 多线程、多进程 (三)之 线程进程对比、多进程

    Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.多线程与多进 ...

  8. JavaSE 软件工程师 认证考试试卷2

    JavaSE 软件工程师 认证考试试卷   笔试   考试时间150分钟 总分 100分   姓    名_______________________ 身份证号___________________ ...

  9. CSS3属性-webkit-font-smoothing字体抗锯齿渲染

    对字体进行抗锯齿渲染可以使字体看起来会更清晰舒服.在图标字体成为一种趋势的今天,抗锯齿渲染使用也越来越多. font-smoothing是非标准的CSS定义.它被列入标准规范的草案中,后由于某些原因从 ...

  10. css div相对屏幕永远居中

    不管屏幕如何滑动,该div始终保持在屏幕正中央(支持IE7(包括IE7)以上版本) <div class="loginBox"></div> .loginB ...