Codeforces#498F. Xor-Paths(折半搜索)
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).
题意:从$(1, 1)$走到$(n, m)$,路径上权值异或起来为$k$的有几条
昨晚前五题都1A之后有点上天qwq。。想了很久才发现这是个思博题不过没时间写了qwq。
考虑如果直接dfs的话是$2^{n + m}$
然后meet in the middle 一下就好了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#define MP(x, y) make_pair(x, y)
#define Pair pair<int, int>
#define int long long
using namespace std;
const int MAXN = * 1e5 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M, K;
int a[][];
cc_hash_table<int, int> mp[];
int dfs(int x, int y, int now) {
if(x < || x > N || y < || y > M) return ;
if(x + y == (N + M + ) / ) return mp[x][now ^ a[x][y]];
int ans = ;
ans += dfs(x - , y, now ^ a[x - ][y]);
ans += dfs(x, y - , now ^ a[x][y - ]);
return ans;
}
void fuck(int x, int y, int now) {
if(x < || x > N || y < || y > M) return ;
if(x + y == (N + M + ) / ) {mp[x][now]++; return ;}
fuck(x + , y, now ^ a[x + ][y]);
fuck(x, y + , now ^ a[x][y + ]);
}
main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
N = read(); M = read(); K = read();
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
a[i][j] = read();
fuck(, , a[][]);
printf("%lld", dfs(N, M, K ^ a[N][M]));
}
/*
1 1 1000000000000000000
1000000000000000000
*/
Codeforces#498F. Xor-Paths(折半搜索)的更多相关文章
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- codeforces 1006 F(折半搜索)
F. Xor-Paths time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- CF 888E Maximum Subsequence——折半搜索
题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...
- 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)
[LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...
- 2018.11.01 NOIP训练 某种密码(折半搜索)
传送门 直接折半搜索,把所有和装到unorderedmapunordered_mapunorderedmap里面最后统计答案就行了. 然后考试的时候读优并没有处理有负数的情况于是爆零了 代码
- [折半搜索][哈希]POJ1186方程的解数
题目传送门 这道题明显N数据范围非常小,但是M很大,所以用折半搜索实现搜索算法的指数级优化,将复杂度优化到O(M^(N/2)). 将搜出的两半结果用哈希的方式合并(乘法原理). Code: #incl ...
- 折半搜索【p4799】[CEOI2015 Day2]世界冰球锦标赛
Description 今年的世界冰球锦标赛在捷克举行.Bobek 已经抵达布拉格,他不是任何团队的粉丝,也没有时间观念.他只是单纯的想去看几场比赛.如果他有足够的钱,他会去看所有的比赛.不幸的是,他 ...
- POJ3977:Subset——题解(三分+折半搜索)
http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...
随机推荐
- ORACLE SQL 实现IRR的计算
一.IRR计算的原理: 内部收益率(Internal Rate of Return (IRR)),就是资金流入现值总额与资金流出现值总额相等.净现值等于零时的折现率. 用公式 标识:-200+[30/ ...
- 《nginx 二》深入理解nginx的各项配置
Nginx应用场景 1.http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器. 2.虚拟主机.可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机. 3. ...
- TouchSlide 触屏滑动特效插件
TouchSlide 是纯javascript打造的触屏滑动特效插件,面向手机.平板电脑等移动终端,能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. 插件开源.体积小.简单实用.功能强大,是 ...
- 根据时间显示不同的问候语的JavaScript代码
对于最近有许多的初学开发者问我关于根据时间显示不同的问候语的JavaScript代码问题,所以今天将自己整理的一些代码在这里分享出来,供初学者参考,如果在运行过程中有问题,可以给我在下方留言. < ...
- GDI+图形图像处理技术——GDIPlus绘图基础
GDI+概述 GDI在windows中定义为Graphis Device interface,及图形设备接口,是Windows API(application Programming Interfac ...
- EF删除数据
1.方法一,面向对象 using (MyDbContent content = new MyDbContent()) { content.Entry<UserInfo>(model).St ...
- SQLSERVER 2012的多维数据库浏览 ,不能多维的显示
网上搜索后发现,原来ssms2012不支持这种方式,要使用Excel的方式 参考地址:http://www.flybi.net/question/12567
- CSS选择器备忘录
CSS选择器备忘录 基本选择器 Selector Meaning Example 通用选择器 匹配任何元素 * 标签选择器 CSS1中称之为元素选择器,匹配为指定标签的所有元素 div 伪元素选择器 ...
- VueJs组件prop验证简单理解
今天看了vuejs的组件,看到了prop组件,主要作用是在传入数据的时候对传入的值做判断,写了个小例子. <div id="app"> <my-child :nu ...
- atom markdown转换PDF 解决AssertionError: html-pdf: Failed to load PhantomJS module
atom编辑器markdown转换PDF 解决AssertionError: html-pdf: Failed to load PhantomJS module. You have to set th ...