codeforces 1006 F(折半搜索)
3 seconds
256 megabytes
standard input
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.
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).
Print one integer — the number of paths from (1,11,1) to (n,mn,m) with xor sum equal to kk.
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
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=,f=;char c=getchar();
while(c>''||c<''){if(x=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void dfs(int dep,int x,int y,ll sta)
{
if(x< || x>n || y< || 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+,y,sta);dfs(dep,x,y+,sta);
}
else{
sta^=a[x][y];
dfs(dep,x-,y,sta);dfs(dep,x,y-,sta);
}
} int main()
{
n=read();m=read();k=read();
for(int i=;i<=n;i++) for(int j=;j<=m;j++)
a[i][j]=read();
flag=;dfs((n+m+)/,,,);
flag=;dfs((n+m+)/,n,m,);
printf("%lld\n",ans);
return ;
}
codeforces 1006 F(折半搜索)的更多相关文章
- Codeforces 1006 F - Xor-Paths
		F - Xor-Path 思路: 双向搜索dfs 如果普通的搜索复杂度是n 那么双向搜索复杂度是√n 代码: #include<bits/stdc++.h> using namespace ... 
- Codeforces#498F. Xor-Paths(折半搜索)
		time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ... 
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
		Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ... 
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
		E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ... 
- 折半搜索【p4799】[CEOI2015 Day2]世界冰球锦标赛
		Description 今年的世界冰球锦标赛在捷克举行.Bobek 已经抵达布拉格,他不是任何团队的粉丝,也没有时间观念.他只是单纯的想去看几场比赛.如果他有足够的钱,他会去看所有的比赛.不幸的是,他 ... 
- 【BZOJ4800】[CEOI2015 Day2]世界冰球锦标赛 (折半搜索)
		[CEOI2015 Day2]世界冰球锦标赛 题目描述 译自 CEOI2015 Day2 T1「Ice Hockey World Championship」 今年的世界冰球锦标赛在捷克举行.\(Bob ... 
- 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)
		[Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ... 
- 折半搜索+状态压缩【P3067】 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
		Description 给n个数,从中任意选出一些数,使这些数能分成和相等的两组. 求有多少种选数的方案. Input 第\(1\)行:一个整数\(N\) 第\(2\)到\(N+1\)行,包含一个整数 ... 
- 【Luogu】P2962灯Lights(折半搜索)
		题目链接 本意是想学高斯消元,然后一顿乱搞之后学到了一个神奇的搜索方式叫做折半搜索. qwq 就是我先dfs前二分之n个点,然后再dfs后二分之n个点. 然后我dfs后二分之n个点的时候判断一下第一次 ... 
随机推荐
- Spring4 基本使用
			前言 虽然现在基本上是 springboot 的天下了,但是传统的 spring4 在广大的软件企业中仍然占据很大比例.一上手能用,但是要让我从无到有搭一个spring4的开发环境出来,可能会磕磕碰碰 ... 
- C++ Virtual 关键字
			虚函数是C++中用于多态的机制.核心理念就是通过基类访问派生类定义的函数. 基类的析构含糊都必须是virtual的 虚函数只能借助于指针或者引用来达到多态的效果. 前提B类继承与A类 且foo()为虚 ... 
- Atom编辑Markdown文件保存后行尾的空格自动消失的问题解决
			Markdown文件的行尾增加两个空格表示一行结束需要换行. 但保存文件后,行尾的空格自动消失,导致不换行. 解决方法: 1.[Edit]->[Preferences]->[Package ... 
- java 定时备份数据库
			原文:http://www.open-open.com/code/view/1447490829678 /** 操作数据库 */ public class BackupDb { public Stri ... 
- 【转】ubuntu 下安装mongodb php 拓展的方法
			按照上面的方法安装成功之后,写一个 mongodb 的php测试脚本,用来测试是否可以 正确连接上mongodb ,并查询结果. 参考:http://php.net/manual/en/class.m ... 
- 对dispatch_async到主线程的逻辑封装成C/C++接口类型
			背景:代码里面有时候会把将要运行的内容放到主线程里面运行,但假设已经是主线程里面的代码调用dispatch_async的时候偶尔会出现crash,所以就须要推断是否已经在主线程里面了. 通常的做法类似 ... 
- js实现动态删除表格的行或者列-------Day57
			昨天记录了动态加入表格的一行,当然这个一行是指一行数据,也就是说一行多少列也是加上的,而且第几列的内容都能够加入上,先来回想下它的实现的关键点: 1.var row=table.insertRow() ... 
- Deepin-安装git
			sudo apt-get install git 命令介绍(安装软件):apt-get install 命令介绍(Debian系列以管理员运行的前缀):sudo 
- hibernate4中HHH000273的错误
			今天配置hibernate4.发现报 17:55:06,815 INFO AbstractPoolBackedDataSource:522 - Initializing c3p0 pool... co ... 
- 开源yYmVc项目,邀您和我一起开发:)
			打算在闲暇时间写个MVC框架,要有什么功能一步一步边写边加,仿照struts 2 和 spring mvc.假设您感兴趣的话,能够私密我,给您加入key:). 欢迎您的到来~ 项目放在基于GIT的CS ... 
