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. ————————————— ...
随机推荐
- Murano Weekly Meeting 2015.08.25
Meeting time: 2015.August.25th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting summ ...
- Python LoggerAdpater类
Logger子类: import logging # create loggermodule_logger = logging.getLogger('spam_application.auxiliar ...
- 60、Docker 学习笔记(CentOS 7.1)
#基本概念 -x86_64-minimal.tar.gz | docker import - centos:v7.mini``` 然后查看导入的镜像: ##上传镜像 >用户可以通过 docker ...
- Visual Studio 要求导入 pfx 密钥以及导入后依然要求导入的解决办法
本文为个人博客备份文章,原文地址: http://validvoid.net/visual-studio-pfx-import/ 导入密钥 在使用 Visual Studio 生产项目时,使用 pfx ...
- canvas的isPoinInPath API实现交互
- canvas绘制圆环
- C++基础--sizeof和strlen的区别
首先,来运行一段程序: #include "stdafx.h" #include <stdio.h> #include <string.h> int mai ...
- Java—多态
多态——对象的多种形态(继承是多态实现的基础) 引用多态:父类的引用可以指向本类的对象:父类的引用可以指向子类的对象 方法多态:创建本类对象时,调用的方法为本类方法:创建子类对象时,调用的方法为子类重 ...
- 百度web应用诉讼费计算器
以前百度推开放平台的时候,利用jquery+jqueryUI做了一个诉讼费计算器,托管在BAE上.闲来无事,把代码和大家共享一下. 在百度搜索"诉讼费"相关的关键词就能看到: ...
- Spark核心组件
Spark核心组件 1.RDD resilient distributed dataset, 弹性分布式数据集.逻辑上的组件,是spark的基本抽象,代表不可变,分区化的元素集合,可以进行并行操作.该 ...