寒假训练——搜索 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 (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
3 3 11
2 1 5
7 10 0
12 6 4
3
3 4 2
1 3 3 3
0 3 3 2
3 0 1 1
5
3 4 1000000000000000000
1 3 3 3
0 3 3 2
3 0 1 1
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) .
思路:
折半思想,前一半:从位置(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;
再^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的更多相关文章
- 寒假训练——搜索 K - Cycle
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...
- 寒假训练——搜索 E - Bloxorz I
Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...
- 寒假训练——搜索——C - Robot
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- J - Abbott's Revenge 搜索 寒假训练
题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...
- 寒假训练 A - A Knight's Journey 搜索
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- 算法专题训练 搜索a-T3 Ni骑士(ni)
搞了半天八数码弄不出来就只好来打题解 这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...
- 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...
- 2016huasacm暑假集训训练三 G - 还是畅通工程
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...
随机推荐
- #7 Python顺序、条件、循环语句
前言 上一节讲解了Python的数据类型和运算,本节将继续深入,涉及Python的语句结构,相当于Python的语法,是以后编写程序的重要基础! 一.顺序语句 顺序语句很好理解,就是按程序的顺序逻辑编 ...
- 大数据技术之_08_Hive学习_04_压缩和存储(Hive高级)+ 企业级调优(Hive优化)
第8章 压缩和存储(Hive高级)8.1 Hadoop源码编译支持Snappy压缩8.1.1 资源准备8.1.2 jar包安装8.1.3 编译源码8.2 Hadoop压缩配置8.2.1 MR支持的压缩 ...
- Java字符串和容器
String Java.lang.String是Java的字符串类. Srting是一个不可变对象,所有对String修改的操作都需要构造新的String实例. String可以由char数组或字符串 ...
- [JSOI2010] 连通数
Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. Output 输出一行一个整数,表示该图 ...
- jQuery基础教程
1.使用$()函数 $()函数其实是创建了一个jQuery对象. 这个函数接受CSS选择符作为参数,充当一个工厂, 返回包含页面中对应元素的jQuery对象. 所有能在样式表中使用的选择符都可以传给这 ...
- Java并发编程:什么是CAS?这回总算知道了
无锁的思想 众所周知,Java中对并发控制的最常见方法就是锁,锁能保证同一时刻只能有一个线程访问临界区的资源,从而实现线程安全.然而,锁虽然有效,但采用的是一种悲观的策略.它假设每一次对临界区资源的访 ...
- HTML5的DeviceOrientation实现微信摇一摇功能
在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...
- python的四大函数讲解
Python的四类函数: 1.普通函数 2.默认函数 3.关键字函数 4.收集参数 1.普通函数 a.定义的时候直接定义变量名 b.调用的时候直接把变量或者值放入指定位置 def 函数名(参数1,参数 ...
- 2018-01-17 Antlr4实现简单语言之整数比较表达式
续上文Antlr4: 修改语法规则更接近普通BNF格式. 例程 为先=1 为先 为2 => 返回false '为'作为关键词, 与数字可以连写, 但必须与变量名用空格间隔: 变量一=1 变量二= ...
- 【代码笔记】Web-HTML-表格
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...